package com.hymjweb.demo.hymj.cash_set_manage.service;

import com.hymjweb.demo.framework.cache.service.GlobalCacheService;
import com.hymjweb.demo.framework.result.MIniUIGridResultMap;
import com.hymjweb.demo.framework.sys.BaseService;
import com.hymjweb.demo.framework.sys.SysConstant;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.DateUtil;
import com.hymjweb.demo.hymj.basic_data_manage.xt_airport.goods_site.bean.XtAirportGoodsSitePojo;
import com.hymjweb.demo.hymj.cash_set_manage.bean.BCashSetPojo;
import com.hymjweb.demo.hymj.cash_set_manage.mapper.BCashSetMapper;
import com.hymjweb.demo.hymj.log_manage.aop.annotation.OperationLogger;
import com.hymjweb.demo.hymj.system_maintenance.user.bean.XtUserPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class BCashSetService extends BaseService {
    @Autowired
    private BCashSetMapper bCashSetMapper;

    @Autowired
    private GlobalCacheService globalCacheService;


    public List<BCashSetPojo> findCacheAll(Map map) {
        return bCashSetMapper.findAll(map);
    }


    public List<BCashSetPojo> queryNoPage(Map map){
        List<BCashSetPojo> list = bCashSetMapper.findAll(map);
        return list;
    }

    public Object getPageList(Map map) {
        this.pageListInit(map);
        List<BCashSetPojo> list = queryNoPage(map);
        MIniUIGridResultMap resultMap = this.pageToGrid(list);
        return resultMap;
    }

    @OperationLogger(oper = SysConstant.OPER_ADD, operTitle="收银设置-添加" , operMemo = "", classType =  BCashSetPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int insert(BCashSetPojo pojo) throws Exception{
        if(pojo == null){
            throwException("收银设置添加参数错误!");
        }
        pojo.setId(getSysStrId());
        checkPojo(pojo);
        String s = DateUtil.dateToStr(pojo.getStartTime(),"yyyyMMddHHmm");

        BCashSetPojo bCashSetPojo = bCashSetMapper.findUK(pojo.getDepartmentId(), DataTypeUtil.toLong(s), DataTypeUtil.toLong(DateUtil.dateToStr(pojo.getEndTime(),"yyyyMMddHHmm")),null);
        if(bCashSetPojo != null){
            throwException(SysConstant.XT_DIC_ERROR_MSG.cash_set_repeat.getName());
        }
        int flag = bCashSetMapper.insert(pojo);
        if(flag > SysConstant.INT_ZERO){
            globalCacheService.loadBCashSetCache();
        }
        return flag;
    }

    @OperationLogger(oper = SysConstant.OPER_UPDATE, operTitle="收银设置-更新" , operMemo = "", classType =  BCashSetPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int update(BCashSetPojo pojo) throws Exception{
        if(pojo == null){
            throwException("收银设置更新参数错误!");
        }
        checkPojo(pojo);
        BCashSetPojo bCashSetPojo = bCashSetMapper.findUK(pojo.getDepartmentId(), DataTypeUtil.toLong(DateUtil.dateToStr(pojo.getStartTime(),"yyyyMMddHHmm")), DataTypeUtil.toLong(DateUtil.dateToStr(pojo.getEndTime(),"yyyyMMddHHmm")),pojo.getId());
        if(bCashSetPojo != null){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.cash_set_repeat.getName());
        }
        int flag = bCashSetMapper.update(pojo);
        if(flag > SysConstant.INT_ZERO){
            globalCacheService.loadBCashSetCache();
        }
        return flag;
    }

    @OperationLogger(oper = SysConstant.OPER_DELETE, operTitle="收银设置-删除" , operMemo = "", classType =  BCashSetPojo.class)
    @Transactional(rollbackFor = {Exception.class})
    public int delete(String id) throws Exception{
        if(DataTypeUtil.isEmptyStr(id)){
            throw new Exception("收银设置删除参数错误");
        }
        int flag = bCashSetMapper.delete(id);
        if(flag > SysConstant.INT_ZERO){
            globalCacheService.loadBCashSetCache();
        }
        return flag;
    }

    /**
     * 检查必输项
     * @param pojo
     */
    private void checkPojo(BCashSetPojo pojo) throws Exception{
        DataTypeUtil.isEmptyStrThenThrow(pojo.getStartTime(),SysConstant.XT_DIC_ERROR_MSG.no_department.getName());
        DataTypeUtil.isEmptyStrThenThrow(pojo.getStartTime(),SysConstant.XT_DIC_ERROR_MSG.no_start_time.getName());
        DataTypeUtil.isEmptyStrThenThrow(pojo.getEndTime(),SysConstant.XT_DIC_ERROR_MSG.no_end_time.getName());

        if(pojo.getStartTime().getTime() > pojo.getEndTime().getTime()){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.start_time_over_end_time.getName());
        }
        Long s = (pojo.getEndTime().getTime() - pojo.getStartTime().getTime()) / (1000 * 60);

        if(s > 2880){
            throw new Exception(SysConstant.XT_DIC_ERROR_MSG.end_time_over_start_time_48.getName());
        }
    }

    /**
     * 根据登录用户的部门，与当前时间，检索当前的收银员对象
     * @param departmentId
     * @param now
     * @return
     */
    public BCashSetPojo queryCashSetByDeptIdAndTime(String departmentId, Date now) {
        if(DataTypeUtil.isEmptyStr(departmentId)){
            return null;
        }
        return bCashSetMapper.queryCashSetByDeptIdAndTime(departmentId,now);
    }


    public List<Map<String,Object>> queryCashierByCondition(String key) {
        List<Map<String,Object>> list = bCashSetMapper.queryCashierByCondition(key);
        List<Map<String,Object>> resultList = DataTypeUtil.lineMapToHumpMap2(list);
        return resultList;
    }
}
